#include "stm32f10x.h"
#include "misc.h"
#include "stm32f10x_rcc.h"
#include "USB_Events.h"
#include "USB_HW_Init.h"
#include "usb_lib.h"
#include "usb_desc.h"
#include "usb_istr.h"
#include "usb_pwr.h"

extern DEVICE_INFO	Device_Info;

/* USB handler */
void OTG_FS_IRQHandler(void)
{

  STM32_PCD_OTG_ISR_Handler(); 

}

/*******************************************************************************
* Function Name  : USB_Cable_Config
* Description    : Software Connection/Disconnection of USB Cable
* Input          : None.
* Return         : Status
*******************************************************************************/
void USB_Cable_Config (FunctionalState NewState)
{

	if (NewState != DISABLE)
	{
		USB_DevConnect();
	}
	else
	{
		USB_DevDisconnect();
	}

}


/*******************************************************************************
* Function Name  : USB_OTG_BSP_uDelay.
* Description    : provide delay (usec).
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_OTG_BSP_uDelay (const uint32_t usec)
{
	RCC_ClocksTypeDef  RCC_Clocks;  

	/* Configure HCLK clock as SysTick clock source */
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  
	RCC_GetClocksFreq(&RCC_Clocks);
  
	SysTick_Config(usec * (RCC_Clocks.HCLK_Frequency / 1000000));  
  
	SysTick->CTRL  &= ~SysTick_CTRL_TICKINT_Msk ;
  
	while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
}



/*******************************************************************************
* Function Name  : Get_SerialNum.
* Description    : Create the serial number string descriptor.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Get_SerialNum(void)
{
	uint32_t Device_Serial0, Device_Serial1, Device_Serial2;

	Device_Serial0 = *(__IO uint32_t*)(0x1FFFF7E8);
	Device_Serial1 = *(__IO uint32_t*)(0x1FFFF7EC);
	Device_Serial2 = *(__IO uint32_t*)(0x1FFFF7F0);

	Device_Serial0 += Device_Serial2;

	if (Device_Serial0 != 0)
	{
		IntToUnicode (Device_Serial0, &USB_StringSerial[2] , 8);
		IntToUnicode (Device_Serial1, &USB_StringSerial[18], 4);
	}
}


/*******************************************************************************
* Function Name  : HexToChar.
* Description    : Convert Hex 32Bits value into char.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
{
	uint8_t idx = 0;
  
	for( idx = 0 ; idx < len ; idx ++)
	{
		if( ((value >> 28)) < 0xA )
		{
			pbuf[ 2* idx] = (value >> 28) + '0';
		}
		else
		{
			pbuf[2* idx] = (value >> 28) + 'A' - 10; 
		}
    
		value = value << 4;
    
		pbuf[ 2* idx + 1] = 0;
	}

}


/*******************************************************************************
* Function Name  : Enter_LowPowerMode
* Description    : Power-off system clocks and power while entering suspend mode
* Input          : None.
* Return         : None.
*******************************************************************************/
void Enter_LowPowerMode(void)
{

	/* Set the device state to suspend */
	bDeviceState = SUSPENDED;

}

/*******************************************************************************
* Function Name  : Leave_LowPowerMode
* Description    : Restores system clocks and power while exiting suspend mode
* Input          : None.
* Return         : None.
*******************************************************************************/
void Leave_LowPowerMode(void)
{
	DEVICE_INFO *pInfo = &Device_Info;

	/* Set the device state to the correct state */
	if (pInfo->Current_Configuration != 0)
	{
		/* Device configured */
		bDeviceState = CONFIGURED;
	}
	else
	{
		bDeviceState = ATTACHED;
	}

}
